package net.zjitc.dao.impl;

import net.zjitc.dao.Userdao;
import net.zjitc.domain.User;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Administrator on 2017/2/27.
 */

@Repository
public class UserDaoImpl implements Userdao {
protected  Session getSession(){
    Session session=sessionFactory.getCurrentSession();
    return session;
}
@Resource

private  SessionFactory sessionFactory;
    @Override
    public boolean findUsernamepassword(String username,String password) {

/*
Session session=sessionFactory.openSession();
Transaction transaction=session.beginTransaction();
*/
User user=(User)getSession().createQuery("from User u where u.username=?")
        .setParameter(0,username)
        .uniqueResult();
        if (user!=null){
            /*transaction.commit();
            session.close();*/
            return true;
        }
        user = new User();
        user.setUsername(username);
        user.setPassword(password);
        getSession().save(user);
        return false;
    }

    @Override
    public List<User> findall() {

        return getSession().createQuery("from User ").list();
    }


}
